import { IEventHandler } from "../Interfaces/IUtils";
import { IProgressManager } from "../Interfaces/IGameManager";
import ADQ from "./ADQ";

/**
 * 进度管理器
 *
 */
export default class ProgressManager implements IProgressManager {
  /**最大值 */
  max: number = 0;
  /**当前值 */
  private cur: number = 0;
  /**固定值 */
  private curfixed: number = 0;
  /**流动值 */
  private curFlow: number = 0;
  /**执行完毕 */
  private compelet: IEventHandler;
  /**实时进度 */
  private realTimeEvent: IEventHandler;

  constructor(compeletFn: IEventHandler, imaxs: number) {
    this.max = imaxs;
    this.compelet = compeletFn;
  }
  /**进度增加 */
  add(num: number = 1) {
    this.curFlow += num;
    this.runDirective();
  }
  setFixed(num: number = 1) {
    this.curfixed = num;
    this.runDirective();
  }
  private runDirective() {
    this.cur = this.curfixed + this.curFlow;
    if (this.realTimeEvent) this.realTimeEvent.runWith([this.cur]);
    if (this.cur >= this.max && this.compelet) this.compelet.run();
  }
  bindProgress(listener: Function, caller?: any) {
    this.realTimeEvent = ADQ.getHandler().create(caller, listener, null, false);
  }
  bindCompelet(listener: Function, caller?: any) {
    this.compelet = listener.bind(caller);
  }
}
